package com.wzh.yuvwater.encoder;

/**
 * 暂停录制管理器
 */
public class PauseManager {

    private static PauseManager sInstance;

    private volatile boolean isPause = false;
    private volatile long pauseTimeLength = 0;  //暂停总时长，单位微秒
    private volatile long lastPauseTime = 0;    //上次暂停时刻，单位微秒

    private PauseManager() {
    }

    public static PauseManager getInstance() {
        if (sInstance == null) {
            synchronized (PauseManager.class) {
                if (sInstance == null)
                    sInstance = new PauseManager();
            }
        }
        return sInstance;
    }

    public boolean isPause() {
        return isPause;
    }

    public long getPauseTimeLength() {
        return pauseTimeLength;
    }

    public long getLastPauseTime() {
        return lastPauseTime;
    }

    public void reset() {
        isPause = false;
        lastPauseTime = 0;
        pauseTimeLength = 0;
        sInstance = null;
    }

    /**
     * 暂停录制
     */
    public void pause() {
        isPause = true;
        lastPauseTime = getMicTime();
    }

    /**
     * 继续录制
     */
    public void resume() {
        isPause = false;
        //本次暂停时长
        long thisPauseTime = getMicTime() - lastPauseTime;
        //总共的暂停时长
        pauseTimeLength = pauseTimeLength + thisPauseTime;
    }

    /**
     * @return返回微秒
     */
    public static Long getMicTime() {
        Long cuTime = System.currentTimeMillis() * 1000; // 微秒
        Long nanoTime = System.nanoTime(); // 纳秒
        return cuTime + (nanoTime - nanoTime / 1000000 * 1000000) / 1000;
    }
}
